package com.browsesoft.htmlcomponent;

import java.util.Hashtable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.browsesoft.dbtools.DBTools;
import com.browsesoft.user.User;

public class HTMLCalendarDBFromModel extends HTMLDBFormModel implements
		HTMLFormModel {
	private String userID = new String();

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		super.service(request, response);
		User user = (User) request.getSession().getAttribute("loginUser");
		userID = user.getID();
	}

	/**
	 * 更新属性
	 * 
	 * @param ht
	 *            存储属性的哈息表
	 */
	public void updateAttributes(Hashtable ht) throws Exception {
		// parentID不为空，说明是新增
		if (parentID != null) {
			// 设置父ID
			attributes.put("userid", userID);
			attributes.put("processid", parentID);
			String[][] result = DBTools
					.executeQueryWithTableHead("select * from " + tableName
							+ " where id=" + (String) attributes.get("id"));
			if (result.length == 1) {
				DBTools.insert(tableName, attributes);
			}
			if (result.length == 2) {
				DBTools.update(tableName, (String) attributes.get("id"),
						attributes);
			}
			parentID = null;
		}
		// 更新
		this.attributes.putAll(ht);
		DBTools.update(tableName, (String) attributes.get("id"), attributes);
	}
}